import isEmpty from 'loadsh/isEmpty'
import { GetInfoPicture, GetMeetingLogInfo, GetPreviewImage, PostDeleteData, PostInfoPicture, PostMeetingLogInfo } from '/constData/requestFun/_httpRequest';
import { GetMeetingMinutes, GetMeetingMinutesById, GetTapeoutMeetingMinutes, GetTapeoutMeetingMinutesById, MeetingMinutesDeleteData, PostMeetingMinutes, PostTapeoutMeetingMinutes } from '/constData/requestFun/_teseRequest';
import compact from 'loadsh/compact'
import { SPACEID } from '/constData/requestFun/_globalData';
Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    TapeoutMeetingInfo: {
      CorpId: '',//申请人
      MeetingNumber: '',//会议编号
      SummaryName: '', //会议纪要名称
      MeetingDate: '',//开会日期
      MeetingAttachments: '[]',//会议相关附件
      MeetingMinutes: '[]',//会议纪要
      Notes: '', //备注
    },
    displayMeetingAttachments: [],
    displayMeetingMinutes: []

  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    MeetingMinutesDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  onNotesChange(date) {
    this.setData({
      TapeoutMeetingInfo: {
        ...this.data.TapeoutMeetingInfo,
        Notes: date
      }
    })
  },
  onSummaryNameChange(date) {
    this.setData({
      TapeoutMeetingInfo: {
        ...this.data.TapeoutMeetingInfo,
        SummaryName: date
      }
    })
  },
  onMeetingNumberChange(date) {
    this.setData({
      TapeoutMeetingInfo: {
        ...this.data.TapeoutMeetingInfo,
        MeetingNumber: date
      }
    })
  },
  handleOk(date, format, e) {
    this.setData({
      TapeoutMeetingInfo: {
        ...this.data.TapeoutMeetingInfo,
        MeetingDate: date
      },
    })
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetTapeoutMeetingMinutes((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              it.item1['displayMeetingAttachments'] = JSON.parse(it.item1.meetingAttachments)
              it.item1['displayMeetingMinutes'] = JSON.parse(it.item1.meetingMinutes)
              return it
            }
          })
          this.setData({
            TapeoutMeetingInfo: {
              ...this.data.TapeoutMeetingInfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    PostTapeoutMeetingMinutes(JSON.stringify(this.data.TapeoutMeetingInfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetTapeoutMeetingMinutesById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.corpId === this.data.TapeoutMeetingInfo.CorpId) {
                it.item1['displayMeetingAttachments'] = JSON.parse(it.item1.meetingAttachments)
                it.item1['displayMeetingMinutes'] = JSON.parse(it.item1.meetingMinutes)
                return it
              }
            })).reverse()
          })
        })
        this.setData({
          TapeoutMeetingInfo: {
            ...this.data.TapeoutMeetingInfo,
            MeetingNumber: '',//会议编号
            SummaryName: '', //会议纪要名称
            MeetingDate: '',//开会日期
            MeetingAttachments: '[]',//会议相关附件
            MeetingMinutes: '[]',//会议纪要
            Notes: '', //备注
          }
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },

  //上传图片
  onUpload() {
    let imageMeetingAttachments = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcMeetingAttachments = this.data.TapeoutMeetingInfo.MeetingAttachments
        imageMeetingAttachments = res.data
        imageMeetingAttachments.push(...JSON.parse(srcMeetingAttachments))
        this.setData({
          displayMeetingAttachments: imageMeetingAttachments,
          TapeoutMeetingInfo: {
            ...this.data.TapeoutMeetingInfo,
            MeetingAttachments: JSON.stringify(imageMeetingAttachments)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传文件
  onUploadFile() {
    let fileMeetingMinutes = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const srcMeetingMinutes = this.data.TapeoutMeetingInfo.MeetingMinutes
        fileMeetingMinutes = res.data
        fileMeetingMinutes.push(...JSON.parse(srcMeetingMinutes))
        this.setData({
          displayMeetingMinutes: fileMeetingMinutes,
          TapeoutMeetingInfo: {
            ...this.data.TapeoutMeetingInfo,
            MeetingMinutes: JSON.stringify(fileMeetingMinutes)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },


  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },

});